﻿using Contour.Contrib.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Attributes;

namespace Contour.Contrib.FieldTypes
{
    public sealed class AyahCaptcha : FieldType
    {
        public AyahCaptchaCustomControl FormControl { get; set; }

        /// <summary>
        ///   Gets or sets a value indicating wheather the label for the control should be hidden.
        /// </summary>
        /// <remarks>
        ///   The captcha control looks fine in the form without a label.
        ///   Additionally, if the captcha control is set to lightbox mode,     
        /// </remarks>
        public override bool HideLabel
        {
            get
            {
                return HideFieldCaption == "checked";
            }
        }

        public AyahCaptcha()
        {
            this.Id = new Guid("db4f5d11-7a22-4065-9549-3b3d6d3c715a");
            this.Name = "Are you a human captcha";
            this.Description = "Creates an Are You a Human captcha";

            // FieldType
            this.Icon = "textfield.png";

            this.DataType = FieldDataType.String;
            this.FormControl = new AyahCaptchaCustomControl();
            this.Value = new List<object>();

        }

        /// <summary>
        /// Gets a value indicating whether SupportsPrevalues.
        /// </summary>
        public override bool SupportsPrevalues
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether SupportsRegex.
        /// </summary>
        public override bool SupportsRegex
        {
            get
            {
                return false;
            }
        }
        
        [Setting("Hide Field Caption", prevalues = "",
            description = "Select to hide the caption for the field. You may want todo this if your captcha is set to lightbox mode.",
            control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
        public string HideFieldCaption { get; set; }

        [Setting("Publisher key", prevalues = "", description = "Publisher Key",
            control = "Umbraco.Forms.Core.FieldSetting.TextField")]
        public string PublisherKey { get; set; }

        [Setting("Scoring key", prevalues = "", description = "Scoring Key",
            control = "Umbraco.Forms.Core.FieldSetting.TextField")]
        public string ScoringKey { get; set; }



        public override WebControl Editor
        {
            get
            {
                this.FormControl.PublisherKey = this.PublisherKey;
                this.FormControl.ScoringKey = this.ScoringKey;

                return this.FormControl;
            }
            set
            {
                base.Editor = value;
            }
        }

        public List<object> Value { get; set; }

        public override List<object> Values
        {
            get
            {
                this.Value.Clear();
                this.Value.Add(string.Empty);
                return this.Value;
            }

            set
            {
                this.Value = value;
            }
        }

        public override string RenderPreview()
        {
            return "<img src=\"/umbraco/plugins/umbracoContour/images/ayah-example.gif\" alt=\"Are you a human verification\" />";
        }

        public override string RenderPreviewWithPrevalues(List<object> prevalues)
        {
            return RenderPreview();
        }
    }
}